import login from '../utils/public.js'
import axios from '../utils/request.js'

let oParams = {}
location.search.slice(1).split('&').forEach((item) => {
    let _arr = item.split('=')
    let k = _arr[0]
    oParams[k] = _arr[1]
})

login().then(user => {
  // 点我解锁答案---------------------------------
  if (oParams.hash) {
    if (user.question_leave_times > 0 || user.question_leave_days > 0) {
      window.location.href = './similarQuestions.html?openid=' + user.openid + '&hash=' + oParams.hash
    }
  }
  



  let vipDayLists = [
    // {
    //   lowPrice: '省1680元',
    //   vipTime: '永久套餐',
    //   price: '￥99',
    //   beforePrice: '￥1779',
    // },
    // { lowPrice: '省0元', vipTime: '31天', price: '￥49', beforePrice: '￥49' },
    // {
    //   lowPrice: '省79元',
    //   vipTime: '95天',
    //   price: '￥69',
    //   beforePrice: '￥148',
    // },
  ]
  let vipTimeLists = []
  var meauList = [
    { text: '优先收录', img: './image/vip_icon.png' },
    { text: '搜索记录', img: './image/search_icon.png' },
    { text: '图片搜索', img: './image/picture_icon.png' },
    { text: '语音搜索', img: './image/voice_icon.png' },
    { text: '极速响应', img: './image/quick_icon.png' },
    { text: '专属客服', img: './image/customer_icon.png' },
  ]
  let dayList = []
  let timeList= []
  function getConfig() {
   
    const data = {}
    axios.get('/getRechargeConfig').then((res) => {
      dayList = res.data.data.vip_days
      timeList = res.data.data.vip_times
  
      dayList.forEach((item) =>{
        vipDayLists.push({
          lowPrice: '省' + (item.price - item.discount_price) / 100 + '元',
          vipTime:item.value + '天',
          value: item.value,
          price: '￥' + item.discount_price / 100,
          beforePrice: '￥' + item.price / 100,
       })
     
      })
      timeList.forEach((item) =>{
        vipTimeLists.push({
          lowPrice: '省' + (item.price - item.discount_price) / 100 + '元',
          vipTime:item.value + '次',
          value: item.value,
          price: '￥' + item.discount_price / 100,
          beforePrice: '￥' + item.price / 100,
       })
     
      })
      $.each(vipDayLists, function (index, item) {
        console.log(vipDayLists,'vipDayLists');
        $('.contentItem')
          .eq(0)
          .append(
            '<div class="vipItem" data-value="'+item.value+'"><div class="lowPrice">' +
              item.lowPrice +
              '</div><div class="priceInfo"> <div class="vipTime">' +
              item.vipTime +
              '</div><div class="price">' +
              item.price +
              '</div> <div class="beforePrice">' +
              item.beforePrice +
              '</div>  <div class="priceTag">不限使用次数</div></div></div>'
          )
      })
      $.each(vipTimeLists, function (index, item) {
        console.log(vipTimeLists,'vipTimeLists');
        $('.contentItem')
          .eq(1)
          .append(
            '<div class="viptimeItem" data-value="'+item.value+'"><div class="lowPrice">' +
              item.lowPrice +
              '</div><div class="priceInfo"> <div class="vipTime">' +
              item.vipTime +
              '</div><div class="price">' +
              item.price +
              '</div> <div class="beforePrice">' +
              item.beforePrice +
              '</div>  <div class="priceTag">不限使用次数</div></div></div>'
          )
      })
      var vipItem = $('.vipItem')
    vipItem.eq(0).addClass('active')
  
    vipItem.click(function (e) {
      var index = $(this).index()
      vipItem.siblings().removeClass('active')
      vipItem.eq(index).addClass('active')
    })
  
  
    var viptimeItem = $('.viptimeItem')
    viptimeItem.eq(0).addClass('active')
  
    viptimeItem.click(function (e) {
      var index = $(this).index()
      viptimeItem.siblings().removeClass('active')
      viptimeItem.eq(index).addClass('active')
    })
    })
   
  
  }
  $(function () {
    getConfig()
    $.each(meauList, function (index, element) {
      $('.meau').append(
        '<div class="meauItem"><div class="imgBack"><img src="' +
          element.img +
          '" alt=""></div><div class="text">' +
          element.text +
          '</div></div>'
      )
    })
    var tabs = $('.tab')
    var tabContents = $('.contentItem')
    // tabs.eq(0).css('color', '#409eff')
    console.log(tabContents)
    for (var i = 0; i < tabs.length; i++) {
      tabs.eq(i).click(function () {
        tabs.removeClass('active')
        this.classList.add('active')
      })
    }
    tabs.click(function (e) {
      var index = $(this).index()
      // tabs.eq(index).css('color', '#409eff')
      // tabs.eq(index - 1).css('color', '#747379')
      tabContents.siblings().addClass('hidden')
      tabContents.eq(index).removeClass('hidden')
    })
  
    var buy = $('.btn')
    buy.on('click', function () {
      const data = {
        vip_type: $('#vip_body').find('.contentItem:not(.hidden)').attr('data-type'),
        vip_value: Number.parseInt($('#vip_body').find('.contentItem:not(.hidden)').find('.active').attr('data-value'))
      }
      axios.post('/buyVip', data).then((res) => {
        onBridgeReady(JSON.parse(res.data.data))
      })
    })
  
    function onBridgeReady(data) {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          appId: data.appId, //公众号ID，由商户传入
          timeStamp: data.timeStamp, //时间戳，自1970年以来的秒数
          nonceStr: data.nonceStr, //随机串
          package: data.packageVal,
          signType: 'RSA', //微信签名方式：
          paySign: data.paySign, //微信签名
        },
        function (res) {
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            // 使用以上方式判断前端返回,微信团队郑重提示：
            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
            window.location.href = './balance.html?openid=' + user.openid
          }
        }
      )
    }
  })
  

})
